function $n(e) {
  return document.querySelector(e);
}
function $na(e) {
  return document.querySelectorAll(e);
}
function fnGetText(e) {
  var t = '';
  //如果传递的是元素，则取其子元素，
  //否则假定它是一个数组
  e = e.childNodes || e;
  //检查所有子节点
  for (var j = 0; j < e.length; j++) {
    //如果它不是一个元素，追加其文本到返回值
    //否则，对其所有子元素递归处理
    t += e[j].nodeType != 1 ?
    e[j].nodeValue : fnGetText(e[j].childNodes);
  }
  //返回所得的文本
  return t;
}
function fdCustomizeColor(el) {
  var img,
  elPatts,
  selector = "";
  var xy = el.dataset.xy.split("-");
  console.log(xy);
  img = fdSetPatternData("PatternEdt", "fill", "");
  if (xy[0] == 0) {
    selector = ".y" + xy[1];
  } else if (xy[1] == 0) {
    selector = ".x" + xy[0];
  } else {
    el.style.background = 'url(images/tiles/' + img + ') center';
  }
  if (!selector)
    return false;
  console.log(33);
  elPatts = $na(selector);
  for (var t = 0; t < elPatts.length; t++) {
    elPatts[t].style.background = 'url(images/tiles/' + img + ') center';
  }
}
(function () {
  fdSetPatternData("Pattern1", '', '');
  var domPattern = $na(".fd-Pattern");
  var flag = false;
  for (var i = 0; i < domPattern.length; i++) {
    domPattern[i].addEventListener('mouseup', function () {
      flag = false;
    }, false);
    domPattern[i].addEventListener('mousedown', function () {
      if(!flag)
        flag = true;
      fdCustomizeColor(this);
    }, false);
    domPattern[i].addEventListener('mouseover', function () {
      if (flag) {
        fdCustomizeColor(this);
      }
    }, false);
  }
})();
function fdSetUserAgent() {
  varpos = navigator.userAgent.indexOf('MSIE');
  if (pos > -1) {
    hiddenValue = document.getElementById('strUserAgent');
    if (hiddenValue) {
      hiddenValue.value = 'MSIE';
    }
  }
  return;
}
function fnCInt(str) {
  var i = parseInt(str);
  return isNaN(i) ? 0 : i;
}
function fdSetDimensions(layout, wa, ha, wb, hb) {
  //Clearoutanycurrentlyentereddimensions
  // for (i = 1; i < 3; i++) {
    // document.getElementById('w' + i + 'a').value = '0';
    // document.getElementById('w' + i + 'ai').value = '0';
    // document.getElementById('h' + i + 'a').value = '0';
    // document.getElementById('h' + i + 'ai').value = '0';
    // if (i > 1) {
      // document.getElementById('w' + i + 'b').value = '0';
      // document.getElementById('w' + i + 'bi').value = '0';
      // document.getElementById('h' + i + 'b').value = '0';
      // document.getElementById('h' + i + 'bi').value = '0';
    // }
  // }

  //Nowshowthespecifiedpresetsize
  document.getElementById('layout' + layout).checked = true;
  document.getElementById('w' + layout + 'a').value = wa;
  document.getElementById('h' + layout + 'a').value = ha;
  if (layout > 1) {
    document.getElementById('w' + layout + 'b').value = wb;
    document.getElementById('h' + layout + 'b').value = hb;
  }
  return;
}

function fdSetPatternStyle(pattern, className, theSelect) {
  theDiv = document.getElementById(pattern + '_' + className + "_" + theSelect.options[theSelect.selectedIndex].value);
  if (theDiv) {
    arrDivs = document.getElementsByTagName('div');
    intDivCount = arrDivs.length;
    for (i = 0; i < intDivCount; i++) {
      testDiv = arrDivs[i];
      if (testDiv.id.indexOf(pattern + '_' + className) == 0) {
        testDiv.style.display = 'none';
        testDiv.style.visibility = 'hidden';
      }
    }
    theDiv.style.display = 'block';
    theDiv.style.visibility = 'visible';
    // theDiv.childNodes[0].click();
    // alert(JSON.stringify(theDiv.childNodes[1]));
    textDiv = fnGetText(theDiv);
    var vData = fdSetPatternData(pattern, className, "")
      vData = vData.slice(vData.indexOf("_"));
    if (vData !== "" && textDiv.indexOf(vData) > -1)
      img = textDiv.substr(0, textDiv.indexOf("_")) + vData;
    else
      img = textDiv.substr(0, textDiv.indexOf(","));
    fdSetPatternColor(pattern, className, img)
  }
  return;
}
function fdSetPatternColor(pattern, className, img) {
  fdSetPatternData(pattern, className, img);
  var selector = ".fd-" + pattern + ".fd-" + className;
  var arrDivs = $na(selector);
  var intDivCount = arrDivs.length;

  for (i = 0; i < intDivCount; i++) {
    var testDiv = arrDivs[i];
    testDiv.style.background = 'url(images/tiles/' + img + ') center';
  }
}
function fdSetPatternData(pattern, className, img) {
  var arrID = ["fill", "border", "checkered"];
  var vData = "",
  kData;
  var selector;
  theCheck = document.getElementById(pattern);
  if (theCheck) {
    theCheck.checked = true;
    if (img) {
      theCheck.dataset[className] = img;
    } else if (className && theCheck.dataset[className]) {
      vData = theCheck.dataset[className];
      return vData;
    } else if (className) {
      return vData;
    }
    for (var item in arrID) {
      kData = arrID[item];
      if (theCheck.dataset[kData]) {
        document.getElementById(kData).value = theCheck.dataset[kData];
      } else {
        selector = ".fd-" + pattern + ".fd-" + kData;
        if ($n(selector))
          document.getElementById(kData).value = $n(selector).dataset["bgImg"];
        else
          document.getElementById(kData).value = "";
      }
    }
  }
  return vData;
}
